jQuery(function(){
	var $ = jQuery;
	var XIFU_XXX = {
		'name' : ['肩宽','胸围','腰围','下摆','袖长','衣长'],
		'type' : ['M', 'L', 'XL', 'XXL'],
		'otype' : ['48/170', '50/175', '52/180', '54/185'],
		'M':[47.2, 112, 106, 116, 62, 78],
		'L':[48.5, 116, 110, 120, 63, 80],
		'XL':[49.5, 120, 114, 124, 64, 82],
		'XXL' : [51.2, 124, 118, 128, 65, 84],
		'value':[[47.2, 48.5, 49.5, 51.2], [112, 116, 120, 124], [106, 110, 114, 118], [116, 120, 124, 128], [62, 63, 64, 65], [78, 80, 82, 84]]
	};
	
	var XIFUConfig = [
		{
			min:140, max:160, condition:[
				{min:40, max:60, result:'M'},
				{min:61, max:70, result:'X'},
				{min:71, max:80, result:'XL'},
				{min:81, max:95, result:'XXL'}
			] 
			
		},
		{
			min:160, max:180, condition:[
				{min:40, max:60, result:'M'},
				{min:61, max:70, result:'X'},
				{min:71, max:80, result:'XL'},
				{min:81, max:95, result:'XXL'}
			]
			
		},
		{
			min:180, max:190, condition:[
				{min:40, max:60, result:'M'},
				{min:61, max:70, result:'X'},
				{min:71, max:80, result:'XL'},
				{min:81, max:95, result:'XXL'}
			]
			
		}
	];

	var h1_s = ' <table class="count_table" border="0" cellspacing="0" cellpadding="0">' +
				'<tr>';
	
	var h1_e = '</tr></table>';
	
	var height_weight = '<td>身高：</td>' + 
					'<td><input type="text" id="height_input"/></td>' +
					'<td class="pad_rt_30">CM(163-188)</td>' +
					'<td>体重：</td>' +
					'<td><input type="text" id="weight_input"/></td>' +
					'<td>KG(40-95)</td>' +
					'<td class="pad_rt_30"><a href="#" class="count_btn" id="count_btn"></a></td>'+
					'<td class="result"><span id="count_res"></span></td>';
	
	var h2_s = '	<div class="size_table">' +
			'<table border="0" cellspacing="0" cellpadding="0">';
	var h2_e = '</table></div>';
	
	var h2_head_s = '<tr><th class="s_tit"></th>';
	var h2_head_e = '</tr>';
	
	var allHtml;
	var config = XIFUConfig;
	var xxx = XIFU_XXX;
	
	function renderH2Head(){
		var type = xxx.type;
		var otype = xxx.otype;
		var tempArr = [];
		
		for(var i = 0, len = type.length; i < len; i++){
			tempArr[i] = '<th>' + type[i] + '<br/>' + otype[i] + '</th>';
		}
		
		return tempArr.join('');
	}
	
	function renderH2Body(){
		var s = '<tr>';
		var sd = '<tr class="dark">';
		var e = '</tr>';
		
		var tempArr = [];
		var name = xxx.name;
		var val = xxx.value;
		for(var i = 0, len = name.length; i < len; i++)
		{
			//alert(name + '-si:' + name.length);
			var h;
			if(i % 2 == 0)
			{
				h = s;
			} else {
				h = sd;
			}
			h += '<td>' + name[i] + '</td>';
			
			//alert('i:' + i);
			for(var j = 0, jlen = val[i].length; j < jlen; j++){
				h += '<td>' + val[i][j] + '</td>';

			}
			h += e;
			
			tempArr.push(h);
		}
		return tempArr.join('');
	}
	
	function showResult(info){
		$('#count_res').text(info);
	}
	
	function checkIt(h, w, config){
		var cc = config;
		var len = cc.length;
		//如果小于最小的大于最大的则输入的条件有误
		if(h < cc[0].min || h > cc[len - 1].max) return -1;
		
		for(var i=0; i < len; i++)
		{
			if(h >= cc[i].min && h <= cc[i].max)
			{
				var con = cc[i].condition;
				var jlen = con.length;
				
				if(w < con[0].min || w > con[len - 1].max ) return -2;
				
				for(var j = 0; j < jlen; j++)
				{
					//alert(con[j].result)
					if(w >= con[j].min && w <= con[j].max) return con[j].result;
				}
			} else {
				continue;
			}	
		}
	}
	
	function bindEvt(){
		var height_in = $('#height_input');
		var weight_in = $('#weight_input');
		var count_btn = $('#count_btn');
		var count_res = $('#count_res');
		var checkNum = /[0-9]+/;
		count_btn.click(function(){
			var h_v = height_in.val();
			var w_v = weight_in.val();
			if(h_v == '' || checkNum.test(h_v)) showResult('请填写正确身高');
			
			if(w_v == '' || checkNum.test(w_v)) showResult('请填写正确体重');
			var res = checkIt(h_v, w_v, config);
			if(res == '-1' || res == '-2') {
				showResult('您输入的数据有误');
			} else {
				
				showResult('我们建议您选择' + res + '尺码');
			}
			return false;
		});
	}
	
	allHtml = h1_s + height_weight + h1_e + h2_s + h2_head_s + renderH2Head() + h2_head_e + renderH2Body() + h2_e;
	$('#counter').html(allHtml);
	bindEvt();
});